home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / fgdemo10.zip / EDITOR.C < prev    next >
Text File  |  1991-10-05  |  29KB  |  1,009 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  editor.c -- this is a primitive graphics mode editor with word-wrap *
  4. *  It can be used as a template for a graphics mode editor, but it is  *
  5. *  not certified bug-free, and anybody thinking of using this code     *
  6. *  should study it carefully, beta-test it thoroughly, and debug as    *
  7. *  appropriate.  This code is presented here primarily for academic    *
  8. *  purposes: it is one approach to the problem of how to write a       *
  9. *  graphics mode text editor.                                          *
  10. *                                                                      *
  11. *  Use this code freely, and send suggestions for optimizations and    *
  12. *  improvements to Diana Gruber.                                       *
  13. *                                                                      *
  14. \**********************************************************************/
  15.  
  16. #include "defs.h"
  17.  
  18. #define CTRL_Y      25
  19.  
  20. #define F1          59
  21. #define F2          60
  22. #define F9          67
  23. #define F10         68
  24.  
  25. #define CHAR_WIDTH   8
  26.  
  27. int overstrike;
  28. int top_row, bottom_row;
  29.  
  30. /**********************************************************************\
  31. *                                                                      *
  32. *  can_wrap -- is there enough room to do a word wrap?                 *
  33. *                                                                      *
  34. \**********************************************************************/
  35.  
  36. can_wrap(s1,s2,length)
  37. char *s1, *s2;
  38. int length;
  39. {
  40.    if (strlen(s1)+strlen(s2) < length)
  41.       return(TRUE);
  42.    else
  43.       return(FALSE);
  44. }
  45.  
  46. /**********************************************************************\
  47. *                                                                      *
  48. *  clear_string -- clear all or part of a row of chars                 *
  49. *                                                                      *
  50. \**********************************************************************/
  51.  
  52. void clear_string(x,y,len,maxx)
  53. int x, y, len, maxx;
  54. {
  55.    int x1, y0;
  56.    int color;
  57.  
  58.    if (len == 0) return;
  59.  
  60.    y0 = y - ptsize;
  61.    x1 = x + len * CHAR_WIDTH - 1;
  62.    if (x1 > maxx) x1 = maxx;
  63.  
  64.    color = fg_getcolor();
  65.    fg_setcolor(background);
  66.    fg_rect(x,x1,y0,y);
  67.    fg_setcolor(color);
  68. }
  69.  
  70. /**********************************************************************\
  71. *                                                                      *
  72. *  editor -- edit nrows lines of text                                  *
  73. *                                                                      *
  74. \**********************************************************************/
  75.  
  76. editor(array,minx,maxx,miny,maxy,nrows,array_len)
  77. char *array;
  78. int  minx, miny, maxx, maxy, nrows;
  79. int  *array_len;
  80. {
  81.    register int i, n;
  82.    int b, l;
  83.    int x, y;
  84.    int iy;
  85.    unsigned char key, aux;
  86.    static char char_string[] = {0,0};
  87.    int maxcol;
  88.    int row;
  89.    char *tempstring;
  90.    char *string[101];
  91.    int col[101];
  92.    int end[101];
  93.    int color;
  94.    int visible_rows;
  95.  
  96.    color = fg_getcolor();
  97.    fg_setpage(visual);
  98.    overstrike = FALSE;
  99.    iy = miny + ptsize;
  100.  
  101.    /* determine the string size */
  102.  
  103.    maxcol = (maxx+1 - minx) / CHAR_WIDTH;
  104.  
  105.    /* allocate space for the strings and initialize all elements to 0 */
  106.  
  107.    for (n = 0; n < nrows; n++)
  108.    {
  109.       if ((string[n] = calloc(maxcol+3,sizeof(char))) == NULL)
  110.          return(ERR);
  111.       col[n] = 0;
  112.       end[n] = 0;
  113.    }
  114.  
  115.    tempstring = calloc(maxcol+2,sizeof(char));
  116.  
  117.    /* display whatever is in the array now */
  118.  
  119.    l = 0;
  120.    x = minx;
  121.    y = iy;
  122.    top_row = 0;
  123.    bottom_row = ((maxy - miny + 1) / (ptsize+1));
  124.    if (bottom_row > nrows) bottom_row = nrows;
  125.  
  126.    /* recalc maxy based on ptsize, for scrolling */
  127.  
  128.    maxy = iy + 1 + (bottom_row-1) * (ptsize + 1);
  129.  
  130.    /* recalc nrows because they don't seem to come out right? */
  131.  
  132.    nrows++;
  133.  
  134.    for (row = top_row; row < bottom_row; row++)
  135.    {
  136.       put_string(&array[l],x,y);
  137.       strcpy(string[row],&array[l]);     /* copy it to the temp string */
  138.       unblank(string[row]);
  139.  
  140.       l += strlen(&array[l]) + 1;
  141.       col[row] = strlen(string[row]);
  142.       y += ptsize + 1;
  143.    }
  144.  
  145.    /* if there any rows beyond the bottom row */
  146.  
  147.    for (row = bottom_row; row<nrows; row++)
  148.    {
  149.       strcpy(string[row],&array[l]);
  150.       l += strlen(&array[l]) + 1;
  151.       unblank(string[row]);
  152.       col[row] = strlen(string[row]);
  153.    }
  154.  
  155.    flushkey();
  156.  
  157.    row = 0;      /* starting position */
  158.    col[row] = 0;
  159.    x = get_x(minx,col[row]);
  160.    y = iy;
  161.    put_editor_cursor(x,y);
  162.  
  163.    visible_rows = bottom_row - top_row;
  164.  
  165.    while (TRUE)
  166.    {
  167.       fg_getkey(&key,&aux);
  168.  
  169.       /* return */
  170.  
  171.       if (key == CR)
  172.       {
  173.          unblank(string[row]);
  174.          remove_editor_cursor(x,y);
  175.          if (row < bottom_row - 1)
  176.          {
  177.             n = end_paragraph(row,string);
  178.             if (n < bottom_row - 1)
  179.             {
  180.                for (i = n; i > row; i--)
  181.                {
  182.                   strcpy(string[i+1],string[i]);
  183.                   y = get_y(iy,i+1);
  184.                   clear_string(minx,y,maxcol,maxx);
  185.                   put_string(string[i+1],minx,y);
  186.                }
  187.                strcpy(string[row+1],&string[row][col[row]]);
  188.                string[row][col[row]] = NULL;
  189.                x = get_x(minx,col[row]);
  190.                y = get_y(iy,row);
  191.                clear_string(x,y,maxcol,maxx);
  192.                y = get_y(iy,row+1);
  193.                clear_string(minx,y,maxcol,maxx);
  194.                put_string(string[row+1],minx,y);
  195.             }
  196.             row++;
  197.          }
  198.          else if (bottom_row < nrows - 1)
  199.          {
  200.             fg_setcolor(background);
  201.             fg_scroll(minx,maxx,(iy+1),maxy,-(ptsize+1),1);
  202.             fg_setcolor(color);
  203.             bottom_row++;
  204.             top_row++;
  205.             row++;
  206.             y = get_y(iy,bottom_row-1);
  207.             put_string(string[bottom_row-1],minx,y);
  208.          }
  209.          else
  210.             row = top_row;
  211.  
  212.          col[row] = 0;
  213.          x = get_x(minx,col[row]);
  214.          y = get_y(iy,row);
  215.          put_editor_cursor(x,y);
  216.       }
  217.  
  218.       /* F1 -- help screen */
  219.  
  220.       else if (aux == F1)
  221.       {
  222.          remove_editor_cursor(x,y);
  223.          help_screen();
  224.          fg_setcolor(15);
  225.          put_editor_cursor(x,y);
  226.       }
  227.  
  228.       /* back space */
  229.  
  230.       else if (key == BS && col[row] > 0)
  231.       {
  232.          remove_editor_cursor(x,y);
  233.          col[row]--;
  234.          string[row][col[row]] = NULL; /* remove a character */
  235.  
  236.          strcpy(tempstring,string[row]);
  237.          strcat(tempstring,&string[row][col[row]+1]);
  238.          strcpy(string[row],tempstring);
  239.  
  240.          x = get_x(minx,col[row]);
  241.          clear_string(x,y,maxcol,maxx);
  242.          put_string(&string[row][col[row]],x,y);
  243.  
  244.          put_editor_cursor(x,y);
  245.       }
  246.  
  247.       /* delete */
  248.  
  249.       else if (aux == DELETE)
  250.       {
  251.          if (string[row][col[row]] == NULL && row < bottom_row-1)
  252.             /* delete at end of line */
  253.          {
  254.             end[row] = strlen(string[row]);
  255.             end[row+1] = strlen(string[row+1]);
  256.  
  257.             b = first_blank(string[row+1]);
  258.             if (end[row] + b <= maxcol)
  259.             {
  260.                remove_editor_cursor(x,y);
  261.  
  262.                /* see if you can wrap more than one word */
  263.  
  264.                while (end[row]+b <= maxcol && b < end[row+1])
  265.                {
  266.                   n = b;
  267.                   b += first_blank(&string[row+1][b+1]) + 1;
  268.                }
  269.                if (end[row]+b > maxcol) b = n;
  270.  
  271.                if (string[row+1][b] == NULL) /* wrap whole line */
  272.                {
  273.                   strcat(string[row],string[row+1]); /* move all lines up */
  274.  
  275.                   for (i = row+1; i < bottom_row; i++)
  276.                   {
  277.                      strcpy(string[i],string[i+1]);
  278.                      y = get_y(iy,i);